In [1]:
import pandas as pd
import plotly.express as px
pd.options.display.max_rows=50
# Load the Utah 1880 Census data
utah_df = pd.read_csv('utah-census-1880.csv',encoding='utf-8')
In [2]:
county_counts= utah_df['county'].value_counts()
In [3]:
# Apply reset_index() to convert the index (county names) into a regular column
county_counts = county_counts.reset_index()
county_counts.columns = ['county', 'count']
# Display the counts after reset_index
county_counts
Out[3]:
| county | count | |
|---|---|---|
| 0 | SALT LAKE | 31989 |
| 1 | UTAH | 17967 |
| 2 | CACHE | 12562 |
| 3 | WEBER | 12358 |
| 4 | SANPETE | 11538 |
| 5 | BOX ELDER | 6766 |
| 6 | DAVIS | 5166 |
| 7 | SUMMIT | 4921 |
| 8 | TOOELE | 4496 |
| 9 | SEVIER | 4457 |
| 10 | WASHINGTON | 4140 |
| 11 | IRON | 4034 |
| 12 | BEAVER | 3864 |
| 13 | MILLARD | 3719 |
| 14 | JUAB | 3477 |
| 15 | KANE | 3090 |
| 16 | WASATCH | 2928 |
| 17 | MORGAN | 1783 |
| 18 | PIUTE | 1616 |
| 19 | UINTAH | 799 |
| 20 | EMERY | 556 |
| 21 | RICH | 281 |
| 22 | SAN JUAN | 204 |
In [4]:
# Create a simple bar chart
fig = px.bar(county_counts, x='county', y='count')
# Show the figure
fig.show()
In [5]:
# Add title and customize axis labels
fig = px.bar(
county_counts,
x='county',
y='count',
title='Population by County in Utah (1880)', # Add a title
labels={'county': 'County', 'count': 'Population'} # Rename axis labels
)
# Display the chart
fig.show()
In [6]:
# Add color and use a cleaner template
fig = px.bar(
county_counts,
x='county',
y='count',
title='Population by County in Utah (1880)',
labels={'county': 'County', 'count': 'Population'},
color='count', # Color bars by population
color_continuous_scale='Inferno_r', # Use a reversed color scale
template='plotly_white' # Use a clean white template
)
# Display the chart
fig.show()
In [7]:
# Add color and use a cleaner template
fig = px.bar(
county_counts,
x='county',
y='count',
title='Population by County in Utah (1880)',
labels={'county': 'County', 'count': 'Population'},
color='count', # Color bars by population
color_continuous_scale='Inferno_r', # Use a reversed color scale and the Inferno color scheme
template='plotly_white' # Use a clean white template
)
# Update layout with additional customizations
fig.update_layout(
xaxis_title='County', # Customize x-axis title
yaxis_title='Number of People', # Customize y-axis title
xaxis_tickangle=-45, # Rotate x-axis labels 45 degrees
height=500, # Set chart height in pixels
width=800, # Set chart width in pixels
title_font=dict(size=22), # Change title font size
plot_bgcolor='white', # Set plot background color
margin=dict(l=40, r=40, t=80, b=80), # Adjust margins (left, right, top, bottom)
showlegend=True, # Show the color scale legend
legend_title_text='Population', # Set legend title
hoverlabel=dict( # Customize hover label appearance
bgcolor="white",
font_size=12,
font_family="Georgia")
)
# Display the chart
fig.show()
In [8]:
# First, let's prepare our data
# Count the frequency of each occupation, take the top 10, and reset the index
top_occupations = utah_df['occupation'].value_counts().head(10).reset_index()
top_occupations.columns = ['occupation', 'count']
# Display the prepared data
top_occupations
Out[8]:
| occupation | count | |
|---|---|---|
| 0 | KEEPING HOUSE | 23031 |
| 1 | AT HOME | 13743 |
| 2 | FARMER | 9535 |
| 3 | LABORER | 6846 |
| 4 | AT SCHOOL | 6549 |
| 5 | MINER | 2596 |
| 6 | WORKS ON FARM | 2026 |
| 7 | CARPENTER | 1225 |
| 8 | SERVANT | 1219 |
| 9 | FARM LABORER | 1065 |
In [9]:
chart= px.bar(top_occupations,
x='occupation',
y='count',
title= 'Top 10 Occupations in Utah (1880)',
labels={'occupation': 'Job', 'count': 'Number of People'},
color='count',
color_continuous_scale='Agsunset',
template='xgridoff',
)
chart.update_layout(
xaxis_title='Job', # Customize x-axis title
yaxis_title='Number of People', # Customize y-axis title
xaxis_tickangle=-45, # Rotate x-axis labels 45 degrees
height=400, # Set chart height in pixels
width=800, # Set chart width in pixels
title_font=dict(size=30), # Change title font size
plot_bgcolor='white', # Set plot background color
margin=dict(l=80, r=40, t=80, b=100), # Adjust margins (left, right, top, bottom)
showlegend=True, # Show the color scale legend
legend_title_text='Number of People', # Set legend title
hoverlabel=dict( # Customize hover label appearance
bgcolor="white",
font_size=12,
font_family="Papyrus")
)
chart.show()
In [ ]:
In [ ]: